import http from '@ohos.net.http'

let request = http.createHttp()

export function get<T>(url:string, params?:Object):Promise<T> {
  return new Promise<T>((resole, reject) => {
    if (params) {
      url += '?' + Object.keys(params).map((k) => `${k}=${params[k]}`).join('&')
    }
    request.request(url, {
      method: http.RequestMethod.GET
    }).then((res) => {
      if (res.responseCode == 200) {
        let t:T = JSON.parse(res.result as string)
        resole(t)
      } else {
        reject(new Error(`网络错误: ${res.responseCode}}`))
      }
    })
      .catch(e => reject(e))
  })
}